"""
Input:
3
Output:
58,91

Input:
4
Output:
34,45,97
"""

def main():
    header, num = input().strip().split()
    n = int(num)

    nodedic = {}
    for _ in range(n):
        addr, data, next = input().strip().split()
        nodedic[addr] = (data, next)

    next = ""
    for _ in range(int(n/2)):
        next = nodedic[header][1]
        header = next

    print(nodedic[header][0])

if __name__ == "__main__":
    main()